package com.example.nsl;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

/**
 * @author ym on 10/14/21
 * 用于监听软键盘显示和隐藏
 */
public class SoftKeyboardUtils {

    /**
     * 根视图高度变化边界值
     */
    private static final int LIMIT_CHANGE = 200;

    /**
     * Activity 的根视图
     */
    private View mRootView;

    /**
     * 记录根视图的显示高度
     */
    private int mRootViewVisibleHeight;

    private OnSoftKeyboardChangeListener changeListener;

    private OnGlobalLayoutListener mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // 获取当前根视图在屏幕上显示的大小
            Rect r = new Rect();
            if (mRootView != null) {
                mRootView.getWindowVisibleDisplayFrame(r);
            }
            int visibleHeight = r.height();

            if (mRootViewVisibleHeight == 0) {
                mRootViewVisibleHeight = visibleHeight;
                return;
            }

            // 根视图显示高度没有变化，可以看作软键盘显示／隐藏状态没有改变。
            if (mRootViewVisibleHeight == visibleHeight) {
                return;
            }

            //根视图显示高度变小超过200，可以看作软键盘显示了。
            if (mRootViewVisibleHeight - visibleHeight > LIMIT_CHANGE) {
                if (changeListener != null) {
                    changeListener.onKeyboardShow(mRootViewVisibleHeight - visibleHeight);
                }
                mRootViewVisibleHeight = visibleHeight;
                return;
            }

            //根视图显示高度变大超过200，可以看作软键盘隐藏了。
            if (visibleHeight - mRootViewVisibleHeight > LIMIT_CHANGE) {
                if (changeListener != null) {
                    changeListener.onKeyboardHide(visibleHeight - mRootViewVisibleHeight);
                }
                mRootViewVisibleHeight = visibleHeight;
                return;
            }
        }
    };

    public SoftKeyboardUtils(Activity activity) {
        // 获取 activity 的根视图
        mRootView = activity.getWindow().getDecorView();
    }

    public interface OnSoftKeyboardChangeListener {
        /**
         * 软键盘显示回调
         */
        void onKeyboardShow(int height);

        /**
         * 软键盘隐藏回调
         */
        void onKeyboardHide(int height);
    }

    public void setOnSoftKeyboardChangeListener(OnSoftKeyboardChangeListener listener) {
        // 去掉软引用是因为在5.0手机上会拿不到软引用的值
        changeListener = listener;
        if (mRootView != null && mRootView.getViewTreeObserver() != null) {
            if (listener != null) {
                // 监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
                mRootView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
            } else {
                mRootView.getViewTreeObserver()
                        .removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
            }
        }
    }

    public void removeChangeListener(){
        removeGlobalLayoutListener();
        changeListener = null;
        mRootView = null;
    }

    private void removeGlobalLayoutListener(){
        if (mRootView == null) {
            return;
        }
        ViewTreeObserver viewTreeObserver = mRootView.getViewTreeObserver();
        if (viewTreeObserver == null){
            return;
        }
        viewTreeObserver.removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
    }
}
